package com.qyer.search.sort;

import com.google.common.collect.Lists;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.exception.SearchException;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.lastminute.SimpleVSKUExtra;
import com.qyer.search.model.lastminute.VSKU;
import com.qyer.search.model.result.SearchEntity;
import com.qyer.search.utils.ListUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by tangzheng on 16/7/22.
 */

public class VPriceTimSorter implements SearchSorter{
  private SearchParameterEntity sp;
  private SearchEntity searchEntity;
  private int orderDirection ;
  private int from;
  private int to;

  public VPriceTimSorter(SearchParameterEntity sp, SearchEntity searchEntity, int orderDirection, int from, int to) {
    this.sp = sp;
    this.searchEntity = searchEntity;
    this.orderDirection = orderDirection;
    this.from = from;
    this.to = to;

  }

  @Override public List<SearchEntity> sort() throws SearchException {
    if(searchEntity == null){
      return new ArrayList<>();
    }
    if(searchEntity.getPackageSize() == 0)
      return new ArrayList<>();

    Map<Integer,Long> sales = MutableCaches.getInstance().PRICE.getCacheContent();
    List<SearchEntity> searchEntities = new ArrayList<>(1);
    List<VSKU> skus, marketSkus;
    skus = searchEntity.getCommonSkus();
    int size = skus.size();
    List<VSKU> unknownList = new ArrayList<>(4);
    List<SimpleVSKUExtra> extraSkus = Lists.newArrayListWithCapacity(size);
    marketSkus = searchEntity.getMarketSkus();
    Collections.sort(marketSkus);
    //skus = searchEntity.getCommonSkus();
    //int size = skus.size();
    List<VSKU> sortSkus = new ArrayList<>(size+10);
    VSKU sku;
    int id;
    long sale;
    for(int i = 0; i < size; i++){
      sku = skus.get(i);
      id = sku.getId();
      if(sales.containsKey(id)) {
        sale = sales.get(id);
        extraSkus.add(SimpleVSKUExtra.build(sku, sale));
      }else {
        unknownList.add(sku);
      }
    }
    if(orderDirection == 0){
      Collections.sort(extraSkus, new Comparator<SimpleVSKUExtra>() {
        @Override public int compare(SimpleVSKUExtra o1, SimpleVSKUExtra o2) {
          return (int)(o1.getExtra_field() - o2.getExtra_field());
        }
      });
    }else if(orderDirection == 1){
      Collections.sort(extraSkus, new Comparator<SimpleVSKUExtra>() {
        @Override public int compare(SimpleVSKUExtra o1, SimpleVSKUExtra o2) {
          return (int)(o2.getExtra_field()- o1.getExtra_field());
        }
      });
    }

    for(SimpleVSKUExtra simpleVSKUExtra : extraSkus){
      sortSkus.add(simpleVSKUExtra.getVsku());
    }

    marketSkus.addAll(sortSkus);
    marketSkus.addAll(unknownList);
    int v_size = marketSkus.size();
    List<VSKU> list = ListUtils.ToSubList(marketSkus, from, to);
    SearchEntity searchEntity = new SearchEntity(list.size());
    searchEntity.addSkus(list);
    searchEntity.setPackageSize(v_size);
    searchEntities.add(searchEntity);

    /*System.out.println("Price SIZE 1:" + v_size);
    System.out.println("Price SIZE from:" + from);
    System.out.println("Price SIZE to:" + to);
    System.out.println("Price SIZE 2:" + list.size());*/

    return searchEntities;
  }
}

